#/bin/bash

# Centos7 Initialization settings

# 检测系统
if [ -e /etc/redhat-release ]; then
	if [[ -z $(grep -i centos /etc/redhat-release|grep '7.[[:digit:]]') ]];then
		   echo -e "\033[1;31mERROR：不支持当前系统,目前只支持centos7系统! \033[0m"
	fi
else
	echo -e "\033[1;31mERROR：未知系统,请确认当前系统是否是redhat系列! \033[0m"
	exit 1
fi
[ $USER != root ] && echo -e "\e[1;31mERROR：执行此脚本需root用户！\e[0m" && exit 1
echo -e "\e[1;34m此脚本为Centos7初始化设置脚本，暂停5秒后开始执行！\n\e[0m"
sleep 5

disk=`df -h | grep ^/dev | awk '{print "名称："$1"\t大小："$2"\t可用："$4}' | column -t`
mem=`free -h|grep ^Mem|awk '{print "总内存："$2"\t空闲："$4"\tbuff/cache："$6}' | column -t`
cpu_num=`grep -c 'model name' /proc/cpuinfo`
ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
echo -e "\e[1;34m本机IP：\n${ip}\n\nCPU核心数：\n${cpu_num}\n\n硬盘：\n${disk}\n\n${mem}\n\e[0m"

echo -e "\e[1;31m此脚本中途会安装多个常用命令，请勿随意中断脚本...\n\e[0m"

# 安装常用命令
cd /etc/yum.repos.d/ && rm -rf CentOS*
curl -o /etc/yum.repos.d/Ali.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
echo -e "\e[1;34m正在配置阿里源\e[0m"
if [ $? = 0 ];then
	echo -e "\e[1;34m阿里源配置完成,正在安装wget命令\e[0m"
	yum -y install wget 1> /dev/null
	echo -e "\e[1;34mwget已安装完成,正在配置epel源\e[0m"
	wget https://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
	echo -e "\e[1;34mepel源配置完成,正在建立本地yum源缓存\e[0m"
	yum clean all &> /dev/null
	yum makecache &> /dev/null
	echo -e "\e[1;34m正在安装vim、bash补全、netstat、tree、nmap、lrzsz、dos2unix、telnet、screen、lsof、ntp请稍后\e[0m"
	yum -y install vim bash-completion net-tools tree nmap lrzsz dos2unix telnet screen lsof ntp &>/dev/null
	echo -e "\e[1;34m查看时间，并设置初始化时间\e[0m"
	date +%F\ %T
	ntpdate cn.pool.ntp.org && hwclock -w
else
	echo -e "\e[1;31mERROR：阿里源配置失败\e[0m"
	exit 2
fi

# 最大文件打开数
ulimit -SHn 65535
ulimit -a &> /dev/null

# 关闭防火墙、selinux
setenforce 0 &>/dev/null
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld &>/dev/null
systemctl disable firewalld &>/dev/null
echo -e "\e[1;31m防火墙,selinux均已经关闭！\e[0m"
sed -ri "s@GSSAPIAuthentication yes@GSSAPIAuthentication no\nUseDNS=no@" /etc/ssh/sshd_config
systemctl restart sshd &>/dev/null

# DNS
cat >> /etc/resolv.conf <<-EOF
#阿里云DNS
nameserver 223.5.5.5
nameserver 223.6.6.6
#百度DNS
nameserver 180.76.76.76
#腾讯DNS
nameserver 119.29.29.29 
#google DNS
nameserver 8.8.8.8
#114 DNS
nameserver 114.114.114.114
nameserver 114.114.115.115
EOF

echo -e "\e[1;32mCentos7初始化执行完成！\e[0m"
